package com.ztorn.fiscale.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ztorn.fiscale.mapper.FiscaleRefundBatchDetailMapper;
import com.ztorn.fiscale.mapper.FiscaleRefundMatchingMapper;
import com.ztorn.fiscale.mapper.FiscaleCustomsDeclarationMapper;
import com.ztorn.fiscale.mapper.FiscaleTaxInvoiceMapper;
import com.ztorn.fiscale.domain.FiscaleRefundBatchDetail;
import com.ztorn.fiscale.domain.FiscaleRefundMatching;
import com.ztorn.fiscale.domain.FiscaleCustomsDeclaration;
import com.ztorn.fiscale.domain.FiscaleTaxInvoice;
import com.ztorn.fiscale.service.IFiscaleRefundBatchDetailService;

/**
 * 退税申报明细Service业务层处理
 *
 * @author ztorn
 * @date 2025-09-20
 */
@Service
public class FiscaleRefundBatchDetailServiceImpl implements IFiscaleRefundBatchDetailService
{
    @Autowired
    private FiscaleRefundBatchDetailMapper fiscaleRefundBatchDetailMapper;

    @Autowired
    private FiscaleRefundMatchingMapper fiscaleRefundMatchingMapper;

    @Autowired
    private FiscaleCustomsDeclarationMapper fiscaleCustomsDeclarationMapper;

    @Autowired
    private FiscaleTaxInvoiceMapper fiscaleTaxInvoiceMapper;

    /**
     * 查询退税申报明细
     *
     * @param detailId 退税申报明细ID
     * @return 退税申报明细
     */
    @Override
    public FiscaleRefundBatchDetail selectFiscaleRefundBatchDetailById(Long detailId)
    {
        FiscaleRefundBatchDetail detail = fiscaleRefundBatchDetailMapper.selectFiscaleRefundBatchDetailById(detailId);
        if (detail != null) {
            // 查询关联的配单信息
            FiscaleRefundMatching matching = fiscaleRefundMatchingMapper.selectFiscaleRefundMatchingById(detail.getMatchingId());
            detail.setMatching(matching);

            // 查询关联的报关单信息
            FiscaleCustomsDeclaration declaration = fiscaleCustomsDeclarationMapper.selectFiscaleCustomsDeclarationByDeclarationId(detail.getDeclarationId());
            detail.setDeclaration(declaration);

            // 查询关联的发票信息
            FiscaleTaxInvoice invoice = fiscaleTaxInvoiceMapper.selectFiscaleTaxInvoiceById(detail.getInvoiceId());
            detail.setInvoice(invoice);
        }
        return detail;
    }

    /**
     * 查询退税申报明细列表
     *
     * @param fiscaleRefundBatchDetail 退税申报明细
     * @return 退税申报明细
     */
    @Override
    public List<FiscaleRefundBatchDetail> selectFiscaleRefundBatchDetailList(FiscaleRefundBatchDetail fiscaleRefundBatchDetail)
    {
        List<FiscaleRefundBatchDetail> list = fiscaleRefundBatchDetailMapper.selectFiscaleRefundBatchDetailList(fiscaleRefundBatchDetail);
        for (FiscaleRefundBatchDetail detail : list) {
            // 查询关联的配单信息
            FiscaleRefundMatching matching = fiscaleRefundMatchingMapper.selectFiscaleRefundMatchingById(detail.getMatchingId());
            detail.setMatching(matching);

            // 查询关联的报关单信息
            FiscaleCustomsDeclaration declaration = fiscaleCustomsDeclarationMapper.selectFiscaleCustomsDeclarationByDeclarationId(detail.getDeclarationId());
            detail.setDeclaration(declaration);

            // 查询关联的发票信息
            FiscaleTaxInvoice invoice = fiscaleTaxInvoiceMapper.selectFiscaleTaxInvoiceById(detail.getInvoiceId());
            detail.setInvoice(invoice);
        }
        return list;
    }

    /**
     * 新增退税申报明细
     *
     * @param fiscaleRefundBatchDetail 退税申报明细
     * @return 结果
     */
    @Override
    public int insertFiscaleRefundBatchDetail(FiscaleRefundBatchDetail fiscaleRefundBatchDetail)
    {
        return fiscaleRefundBatchDetailMapper.insertFiscaleRefundBatchDetail(fiscaleRefundBatchDetail);
    }

    /**
     * 修改退税申报明细
     *
     * @param fiscaleRefundBatchDetail 退税申报明细
     * @return 结果
     */
    @Override
    public int updateFiscaleRefundBatchDetail(FiscaleRefundBatchDetail fiscaleRefundBatchDetail)
    {
        return fiscaleRefundBatchDetailMapper.updateFiscaleRefundBatchDetail(fiscaleRefundBatchDetail);
    }

    /**
     * 批量删除退税申报明细
     *
     * @param detailIds 需要删除的退税申报明细ID
     * @return 结果
     */
    @Override
    public int deleteFiscaleRefundBatchDetailByIds(Long[] detailIds)
    {
        return fiscaleRefundBatchDetailMapper.deleteFiscaleRefundBatchDetailByIds(detailIds);
    }

    /**
     * 删除退税申报明细信息
     *
     * @param detailId 退税申报明细ID
     * @return 结果
     */
    @Override
    public int deleteFiscaleRefundBatchDetailById(Long detailId)
    {
        return fiscaleRefundBatchDetailMapper.deleteFiscaleRefundBatchDetailById(detailId);
    }

    /**
     * 根据批次ID查询退税申报明细列表
     *
     * @param batchId 批次ID
     * @return 退税申报明细集合
     */
    @Override
    public List<FiscaleRefundBatchDetail> selectFiscaleRefundBatchDetailListByBatchId(Long batchId)
    {
        List<FiscaleRefundBatchDetail> list = fiscaleRefundBatchDetailMapper.selectFiscaleRefundBatchDetailListByBatchId(batchId);
        for (FiscaleRefundBatchDetail detail : list) {
            // 查询关联的配单信息
            FiscaleRefundMatching matching = fiscaleRefundMatchingMapper.selectFiscaleRefundMatchingById(detail.getMatchingId());
            detail.setMatching(matching);

            // 查询关联的报关单信息
            FiscaleCustomsDeclaration declaration = fiscaleCustomsDeclarationMapper.selectFiscaleCustomsDeclarationByDeclarationId(detail.getDeclarationId());
            detail.setDeclaration(declaration);

            // 查询关联的发票信息
            FiscaleTaxInvoice invoice = fiscaleTaxInvoiceMapper.selectFiscaleTaxInvoiceById(detail.getInvoiceId());
            detail.setInvoice(invoice);
        }
        return list;
    }
}
